var fs = require('fs');
var express = require('express');
var multer = require('multer');
var router = express.Router()
// var Message = require('../models/message')

//定义存储内容
var storage = multer.diskStorage({
	destination: function (req, file, cb) {
		//设置存储目标路径
		cb(null, 'public/images/upload/')
	},
	//cb指的是callback
	filename: function (req, file, cb) {
		cb(null, file.fieldname + "-" + Date.now() + "-" + file.originalname)
	}
});
var upload = multer({ storage: storage });

//post请求可以这样写
router.post('/upload', upload.single("file"), (req, res, next) => {
	var filename = req.file.filename;
	console.log(filename + "文件名");
	res.json({
		code: 200,
		url: 'images/upload/' + req.file.filename 
	})
})

module.exports = router